/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package give.replay.gui;

import give.comm.ActionMessage;
import give.comm.ClientReadyMessage;
import give.comm.DidNotUnderstandMessage;
import give.comm.LostMessage;
import give.comm.Message;
import give.comm.SuccessMessage;
import give.comm.TextMessage;
import give.replay.GameEvent;
import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author koller
 */
public class EventListCellRenderer implements ListCellRenderer {
    private static DateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");

    public static boolean canRenderMessageOfThisType(Message m) {
        return (m instanceof TextMessage) || (m instanceof ActionMessage) || (m instanceof DidNotUnderstandMessage)
                 || (m instanceof SuccessMessage) || (m instanceof LostMessage);
    }

    public Component getListCellRendererComponent(JList list, Object event, int index, boolean isSelected, boolean hasFocus) {
        GameEvent e = (GameEvent) event;
        Message m = e.message;
        JLabel ret = new JLabel();
        String timestamp = format.format(e.timestamp);

        Color background;
        Color foreground;

        ret.setOpaque(true);

        if( m instanceof TextMessage ) {
            foreground = Color.black;
            background = Color.white;
            ret.setText(timestamp + "   " + ((TextMessage) m).getText());
        } else if( m instanceof ActionMessage ) {
            foreground = Color.blue;
            background = Color.white;
            ret.setText(timestamp + "      -> " + ((ActionMessage) m).getHeader().toString());
        } else if( m instanceof DidNotUnderstandMessage ) {
            foreground = Color.red;
            background = Color.white;
            ret.setText(timestamp + "      -> What??");
        } else if( m instanceof ClientReadyMessage ) {
            foreground = Color.black;
            background = Color.white;
            ret.setText(timestamp + "   (start)");
        } else if( m instanceof SuccessMessage ) {
            foreground = Color.black;
            background = Color.white;
            ret.setText(timestamp + "   (end: success)");
        } else if( m instanceof LostMessage ) {
            foreground = Color.black;
            background = Color.white;
            ret.setText(timestamp + "   (end: lost)");
        } else {
            foreground = Color.red;
            background = Color.white;
            ret.setText(timestamp + "  ??");
        }

        if (isSelected) {
            foreground = Color.white;
            background = Color.blue;
        }

        ret.setBackground(background);
        ret.setForeground(foreground);

        return ret;
    }
}
